package com.ruoyi.accounts.service.base;

import com.ruoyi.accounts.service.IClientHandler;
import com.ruoyi.accounts.tcp.WSCommand;
import com.ruoyi.accounts.tcp.WSRespone;
import com.ruoyi.accounts.tcp.GameStateService;
import com.ruoyi.accounts.tcp.GameStateEnum;
import javax.websocket.Session;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import com.ruoyi.accounts.tcp.TcpThreadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractClientHandler implements IClientHandler{
    
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractClientHandler.class);

    private ObjectMapper objectMapper = new ObjectMapper();

    // 可以添加共享的字段
    @Autowired
    protected StringRedisTemplate stringRedisTemplate;
    
    @Autowired
    protected TcpThreadService tcpThreadService;

    protected void handleCheckCommon(WSCommand command, Session session) throws Exception {
        // 实现check处理逻辑
        if (GameStateService.isTakeUp(command.getCustomID(), command.getMachineID())) {
            sendMessageToSocketClient(session, true, "已占机");
        } else {
            LOGGER.info("handleCheck isTakeUp false");
            sendMessageToSocketClient(session, false, "未占机");
        }
    }

    protected void handleUp(WSCommand command, Session session, GameStateEnum state) throws Exception {
        // 实现娃娃机的up处理逻辑
        // 上
        // 判断游戏状态为play
        if (state == null || !state.equals(GameStateEnum.Playing)) {
            sendMessageToSocketClient(session, false, "游戏状态异常");
            return;
        }
        // 发送消息
        try {
            tcpThreadService.SendUp(command.getCustomID(), command.getMachineID());
            sendMessageToSocketClient(session, true, "成功");
        } catch (Exception e) {
            sendMessageToSocketClient(session, false, e.getMessage());
        }
        GameStateService.refresh(command.getCustomID());
    }

    protected void handleDown(WSCommand command, Session session, GameStateEnum state) throws Exception {
        // 实现娃娃机的down处理逻辑
        // 下
        // 判断游戏状态为play
        if (state == null || !state.equals(GameStateEnum.Playing)) {
            sendMessageToSocketClient(session, false, "游戏状态异常");
            return;
        }
        // 发送消息
        try {
            tcpThreadService.SendDown(command.getCustomID(), command.getMachineID());
            sendMessageToSocketClient(session, true, "成功");
        } catch (Exception e) {
            sendMessageToSocketClient(session, false, e.getMessage());
        }
        GameStateService.refresh(command.getCustomID());
    }
    
    protected void handleLeft(WSCommand command, Session session, GameStateEnum state) throws Exception {
        // 实现娃娃机的left处理逻辑
        // 左
        // 判断游戏状态为play
        if (state == null || !state.equals(GameStateEnum.Playing)) {
            sendMessageToSocketClient(session, false, "游戏状态异常");
            return;
        }
        // 发送消息
        try {
            tcpThreadService.SendLeft(command.getCustomID(), command.getMachineID());
            sendMessageToSocketClient(session, true, "成功");
        } catch (Exception e) {
            sendMessageToSocketClient(session, false, e.getMessage());
        }
        GameStateService.refresh(command.getCustomID());
    }

    protected void handleRight(WSCommand command, Session session, GameStateEnum state) throws Exception {
        // 实现娃娃机的right处理逻辑
        // 右
        // 判断游戏状态为play
        if (state == null || !state.equals(GameStateEnum.Playing)) {
            sendMessageToSocketClient(session, false, "游戏状态异常");
            return;
        }
        // 发送消息
        try {
            tcpThreadService.SendRight(command.getCustomID(), command.getMachineID());
            sendMessageToSocketClient(session, true, "成功");
        } catch (Exception e) {
            sendMessageToSocketClient(session, false, e.getMessage());
        }
        GameStateService.refresh(command.getCustomID());
    }

    protected void sendMessageToSocketClient(Session session, boolean result, String message) throws Exception {
        session.getBasicRemote().sendText(objectMapper.writeValueAsString(new WSRespone(result, message)));
    }

    protected void handleRefund(WSCommand command, Session session) throws Exception {
        try {
            tcpThreadService.SendRefund(command.getCustomID(), command.getMachineID());
            sendMessageToSocketClient(session, true, "成功");
        } catch (Exception e) {
            sendMessageToSocketClient(session, false, e.getMessage());
        }
    }

    // 保留接口方法为抽象，强制子类实现
    @Override
    public abstract boolean canHandle(String commandType);
    
    @Override
    public abstract Long getMachineType();
    
    @Override
    public abstract void handleCommand(WSCommand command, Session session, String message) throws Exception;

}
